<template>
  <div class="new_auction_index">
    <c-title :hide="false" :text="'拍品列表'"></c-title>
    <van-tabs v-model="active" sticky line-width="20px" title-active-color="#F15353" @change="tabChange">
      <van-tab title="全部" :name="0"></van-tab>
      <van-tab :title="item.name" v-for="(item, index) in category_list" :name="item.id" :key="index"></van-tab>
    </van-tabs>
    <div class="list">
      <div class="lis" v-for="item in good_list" :key="item.id" @click="tapDetail(item)">
        <div class="lis_img" v-if="item.has_one_goods">
          <img :src="item.has_one_goods.thumb" alt="" />
        </div>
        <div class="lis_b" v-if="item.has_one_goods">
          <div class="title">{{ item.has_one_goods.title }}</div>
          <div class="price">{{ $i18n.t("money") }}{{ item.now_amount }}</div>
          <div class="countDownt">
            <span class="txt">{{ item.running_status == 0 ? "开拍倒计时：" : "距离结束：" }}</span>
            <van-count-down :time="setTime(item)"> </van-count-down>
          </div>
        </div>
      </div>
      <div class="isnone" v-if="good_list.length <= 0">
        <van-empty image="search" description="暂无更多" />
      </div>
    </div>
    <div class="posBtn" @click="showPopup = true">
      <i class="iconfont icon-icon_dingdan_1"></i>
      <div class="txt">规则</div>
    </div>
    <van-popup v-model="showPopup" round position="bottom" :style="{ height: '60%' }" :close-on-click-overlay="false">
      <div class="optionPopupBox">
        <div class="close">
          <div class="txt">{{ plugin_name }}规则</div>
          <div class="close_b" @click="closeBtn"><van-icon name="cross" color="#6E6E79" /></div>
        </div>
        <div v-html="contenDetail"></div>
      </div>
    </van-popup>
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      active: 0,
      showPopup: false,
      contenDetail: "",
      storeArticle: [],
      good_list: [],
      plugin_name: "",

      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  activated() {
    this.getData();
    let plugin_setting = localStorage.getItem("plugin_setting") ? JSON.parse(localStorage.getItem("plugin_setting")) : "";
    this.plugin_name = plugin_setting.new_auction.plugin_name;
  },
  computed: {
    setTime(item) {
      return function(item) {
        let nowTime = new Date().getTime();
        let rtime = "";
        if (item.running_status == 0) {
          // 未开始
          console.log(item.start_second, nowTime);
          rtime = (item.start_second * 1000 - nowTime).toFixed(0);
        } else if (item.running_status == 1) {
          // 进行中
          rtime = (item.end_second * 1000 - nowTime).toFixed(0);
        }
        console.log(rtime, nowTime);
        return rtime;
      };
    }
  },
  methods: {
    tapDetail(item) {
      this.$router.push(this.fun.getUrl("new_auction_detail", { id: item.id }));
    },
    closeBtn() {
      this.showPopup = false;
    },
    async getData() {
      let { data, msg, result } = await $http.post("plugin.new-auction.frontend.activity.categoryList", {}, "");
      if (result) {
        this.category_list = data.category_list;
        this.active = 0;
        this.contenDetail = data.rule;
        this.init();
        this.getList();
      } else {
        this.$toast(msg);
      }
    },
    init() {
      this.page = 1;
      this.total_page = 0;
      this.isLoadMore = true;
    },
    tabChange() {
      this.init();
      this.getList();
    },
    async getList() {
      let json = {
        category_id: this.active == 0 ? "" : this.active
      };
      let { data, msg, result } = await $http.post("plugin.new-auction.frontend.activity", json, "");
      if (result) {
        this.good_list = data.data;
        this.isLoadMore = true;
        this.total_page = data.last_page;
      } else {
        this.isLoadMore = false;
        this.$toast(msg);
      }
    },
    async getMoreData() {
      if (!this.isLoadMore) return;
      this.isLoadMore = false;
      if (this.data.page >= this.total_page) {
        return;
      } else {
        this.page += 1;
        let json = {
          page: this.page,
          category_id: ""
        };
        let { data, msg, result } = await $http.post("plugin.new-auction.frontend.activity", json, "");
        if (result) {
          this.good_list = this.good_list.concat(data.data);
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
          this.$toast(msg);
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.isnone {
  text-align: center;
  width: 100%;
  overflow: hidden;
}
.new_auction_index {
  background: #f7f7f7;
  min-height: 100vh;
  padding-bottom: 5rem;
}
.optionPopupBox {
  padding: 1.25rem 0.75rem 0 0.75rem;
  box-sizing: border-box;
  height: 100%;
  position: relative;
  .close {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.94rem;
    .txt {
      font-weight: bold;
      font-size: 1rem;
    }
    .close_b {
      width: 1.5rem;
      height: 1.5rem;
      background: #f0f0f1;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}
.posBtn {
  position: fixed;
  bottom: 7.19rem;
  right: 0.75rem;
  width: 2.75rem;
  height: 2.75rem;
  background: #ffffff;
  box-shadow: 0rem 0.06rem 0.19rem 0.03rem #f0f0f1;
  border: 0.03rem solid #d6d6dc;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
}
.list {
  padding: 0.63rem 0.75rem;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  .lis {
    background: #fff;
    border-radius: 0.75rem;
    display: flex;
    flex-direction: column;
    // 设置宽度为50%，确保一行两个元素
    width: calc(50% - 0.315rem); // 减去一半的margin-right以避免换行时总宽度超出
    margin-right: 0.63rem;
    margin-bottom: 0.63rem; // 可选，增加底部外边距以改善视觉效果

    &:nth-child(2n) {
      margin-right: 0; // 每行第二个元素取消右边距
    }
    .lis_img {
      flex-shrink: 0;
      height: 10.66rem;
      background: #c1c1c1;
      border-radius: 0.75rem;
      img {
        width: 100%;
        height: 100%;
        border-radius: 0.75rem;
      }
    }
    .lis_b {
      flex: 1;
      overflow: hidden;
      padding: 0.63rem 0.75rem;
      box-sizing: border-box;
      text-align: left;
      .title {
        font-weight: bold;
        font-size: 0.81rem;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .countDownt {
        color: #f15353;
        font-size: 0.81rem;
        line-height: 0.81rem;
        display: flex;
        align-items: center;
        .txt {
          color: #3b3b4a;
        }
        .van-count-down {
          color: #f15353 !important;
        }
      }
      .price {
        font-weight: 500;
        font-size: 0.94rem;
        color: #f15353;
        line-height: 1rem;
        margin: 0.63rem 0 0.4rem 0;
      }
    }
  }
}
</style>
